package org.jeecg.modules.golf.tag.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.golf.tag.entity.Tag;
import org.jeecg.modules.golf.tag.entity.TagMapping;
import org.jeecg.modules.golf.tag.service.ITagMappingService;
import org.jeecg.modules.golf.tag.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 标签
 * @Author: jeecg-boot
 * @Date:   2020-10-18
 * @Version: V1.0
 */
@Api(tags="标签")
@RestController
@RequestMapping("/tag/")
@Slf4j
public class TagController extends JeecgController<Tag, ITagService> {
	@Autowired
	private ITagService tagService;

	 @Autowired
	 private ITagMappingService tagMappingService;


	 /**
	  * 通过id查询
	  * 通过外键查询
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "标签关联表通过主表ID查询")
	 @ApiOperation(value="标签关联表主表ID查询", notes="标签关联表-通主表ID查询")
	 @GetMapping(value = "queryTagMappingByMainId")
	 public Result<?> queryTagMappingListByMainId(@RequestParam(name="id",required=true) Long id
	 ,@RequestParam(name="mappingType",required=true) Integer mappingType) {
		 List<TagMapping> tagMappingList = tagMappingService.selectByMainId(id,mappingType);
		 return Result.OK(tagMappingList);
	 }


	/**
	 * 分页列表查询
	 *
	 * @param tag
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "标签-分页列表查询")
	@ApiOperation(value="标签-分页列表查询", notes="标签-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Tag tag,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Tag> queryWrapper = QueryGenerator.initQueryWrapper(tag, req.getParameterMap());
		Page<Tag> page = new Page<Tag>(pageNo, pageSize);
		IPage<Tag> pageList = tagService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param tag
	 * @return
	 */
	@AutoLog(value = "标签-添加")
	@ApiOperation(value="标签-添加", notes="标签-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Tag tag) {
		tagService.save(tag);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param tag
	 * @return
	 */
	@AutoLog(value = "标签-编辑")
	@ApiOperation(value="标签-编辑", notes="标签-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Tag tag) {
		tagService.updateById(tag);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "标签-通过id删除")
	@ApiOperation(value="标签-通过id删除", notes="标签-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tagService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "标签-批量删除")
	@ApiOperation(value="标签-批量删除", notes="标签-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tagService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "标签-通过id查询")
	@ApiOperation(value="标签-通过id查询", notes="标签-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Tag tag = tagService.getById(id);
		if(tag==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tag);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tag
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Tag tag) {
        return super.exportXls(request, tag, Tag.class, "标签");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Tag.class);
    }

}
